package com.example.tao.mycar.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVMobilePhoneVerifyCallback;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.utils.RequestUtils;

public class PhoneVerifyActivity extends MyActionBarActivity implements View.OnClickListener {

    private Button mIdentify;
    private Button nextButton;
    private EditText phoneText;
    private EditText phoneIdentityText;
    private String correctPhoneNum;
    boolean canSendRequest = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_phone_verify);
        setToolBar(getResources().getString(R.string.phone_verify), true);

        mIdentify = (Button)this.findViewById(R.id.send_identify);
        nextButton = (Button)this.findViewById(R.id.next);
        phoneText = (EditText)this.findViewById(R.id.iuput_num);
        phoneIdentityText = (EditText)this.findViewById(R.id.identify_num);

        mIdentify.setOnClickListener(this);
        nextButton.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        phoneText.setText("");
        phoneIdentityText.setText("");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_identify :
                String phoneNumStr = phoneText.getText().toString();
                checkLegalPnoneNum(phoneNumStr);
                correctPhoneNum = phoneNumStr;
                if (canSendRequest) {
                    AVOSCloud.requestSMSCodeInBackground(correctPhoneNum, "My Car", null, 2, new RequestMobileCodeCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {
                                Toast.makeText(PhoneVerifyActivity.this, "验证码已经发送，请在2分钟之内完成验证！", Toast.LENGTH_LONG).show();
                            } else {
                                //if (e.getCode() == 601) {
                                //    Toast.makeText(PhoneVerifyActivity.this, "发送短信过快，请稍后重试！", Toast.LENGTH_LONG).show();
                                //}
                                e.printStackTrace();
                            }
                        }
                    });
                }
                break;
            case R.id.next :
                String identifyCode = phoneIdentityText.getText().toString();
                if (!TextUtils.isEmpty(identifyCode)) {
                    verifySMSCode(identifyCode, correctPhoneNum);
                }
                break;
            default:
                break;
        }
    }

    private void verifySMSCode(String code, String phone) {
        AVOSCloud.verifySMSCodeInBackground(code, phone, new AVMobilePhoneVerifyCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    RequestUtils.getSharedPreferenceEditor(PhoneVerifyActivity.this).putString(RequestUtils.USERNAME, correctPhoneNum).commit();
                    Intent intent = new Intent(PhoneVerifyActivity.this, SetPasswordActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                } else {
                    e.printStackTrace();
                }
            }
        });
    }

    private void checkLegalPnoneNum(String phoneNumStr) {
        if (TextUtils.isEmpty(phoneNumStr)) {
            canSendRequest = false;
            Toast.makeText(this, R.string.input_phone_number, Toast.LENGTH_LONG).show();
        } else if (!phoneNumStr.isEmpty() && !phoneNumStr.matches("^(13|14|15|17|18)\\d{9}$")) {
            canSendRequest = false;
            Toast.makeText(this, R.string.input_legal_phone_number, Toast.LENGTH_LONG).show();
        }
    }
}
